<!--待办任务页面 Form表单-->
<template>
    <div v-loading="loading">
        <div class="topsubmint el-col el-col-21">审核反馈
            <el-button style="float: right; margin-right:50px;" type="primary" @click="submitForm" v-if="BILL_STATE=='需理赔修改'">提交</el-button>
    
        </div>
        <el-card class="box-card">
            <div slot="header" class="card-heaer">
                <span style="line-height: 36px;">账单录入</span>
    
            </div>
            <el-form label-width="100px" v-if="!isediter">
                <el-row type="flex" class="row-bg">
                    <el-col :span="5">
    
                        <el-form-item label="医院名称:">
                            <el-input v-model="BILL_HISPOTAL" placeholder="医院名称"></el-input>
                        </el-form-item>
    
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="疾病名称:">
                            <el-input v-model="BILL_DISEASE" placeholder="疾病名称"></el-input>
                        </el-form-item>
                    </el-col>    
                </el-row>
                 <el-row type="flex" class="row-bg">
                       <el-col :span="6">
                        <el-form-item label="疾病详情:">
                            <el-input v-model="BILL_DISEASEDETAIL" type="textarea" placeholder="疾病详情"></el-input>
                        </el-form-item>
                    </el-col>
                 </el-row>
                <el-row type="flex" class="row-bg">
                    <el-col :span="6">
                        <el-form-item label="日期:">
                            <el-date-picker type="date" placeholder="选择日期" v-model="addtime" style="width: 100%;"></el-date-picker>
                        </el-form-item>
                    </el-col>
    
                    <el-col :span="6">
                        <el-form-item label="医院类型:">
                            <el-radio-group v-model="BILL_DATA_TYPE">
                                <el-radio label="网络内"></el-radio>
                                <el-radio label="网络外"></el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="3">
                        <el-button type="primary" @click="adddata">新增一天</el-button>
                    </el-col>
                    <el-col :span="3">
    
                        <el-button type="primary" class="fileselect">
                            <input type="file" @change="upload" value="" id="fileup"> 上传附件
                        </el-button>
                    </el-col>
                </el-row>
            </el-form>
    
            <div class="space20">
            </div>
            <el-form>
    
                <el-form-item label="原因">
                    <el-input v-model="BILL_WHY" type="textarea" style="width:500px" :disabled="true"></el-input>
                </el-form-item>
            </el-form>
    
            <div class="space20">
            </div>
            <el-collapse v-if="bill.length>0 && !isediter">
                <el-collapse-item :title="data.show+'账单'" :key="data.BILL_DATA_DATA" v-for="(data, index) in bill ">
    
                    <el-card class="box-card">
                        <div slot="header" class="card-heaer">
                            <span style="line-height: 36px;">{{data.show}}账单</span>
                            <el-button style="float: right; margin-right:50px;" type="danger" @click="delectdata(data)">删除</el-button>
    
                        </div>
                        <el-form label-width="200px">
                            <el-row type="flex" class="row-bg">
                                <el-col :span="8">
                                    <el-form-item label="医院类型:">
                                        <el-radio-group v-model="data.BILL_DATA_TYPE">
                                            <el-radio label="网络内"></el-radio>
                                            <el-radio label="网络外"></el-radio>
                                        </el-radio-group>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="8">
    
                                </el-col>
                            </el-row>
                        </el-form>
                        <div class="space20">
                        </div>
                        <liepeiup_item :billdataid="index" :isediter="isediter" ref="dataform">
                        </liepeiup_item>
                    </el-card>
                </el-collapse-item>
            </el-collapse>
    
            </el-collapse>
        </el-card>
    </div>
</template>
<script>
import {
    mapState
} from 'vuex';
import getid from '@/js/getid';
import liepeiup_item from '@/components/lipei/lipeitaskform_item'

export default {
    data() {
        return {
            addtime: "",
            file: "",
            loading: false,
            isediter: false,
            BILL_DATA_HNAME: "",
            BILL_DATA_TYPE: "",
            BILL_DISEASEDETAIL: "",
            BILL_DISEASE: "",
            BILL_STATE: "",
            BILL_WHY: "",
            BILL_HISPOTAL: ""
        };
    },
    components: {
        'liepeiup_item': liepeiup_item


    },
    computed: mapState({
        bill: state => state.bill.bill
    }),
    methods: {
        upload(event) {
            let file = event.target.files[0];
            // if (file.type != "application/x-zip-compressed") {
            //     this.$notify({
            //         title: '文件格式不正确',
            //         message: '仅支持zip格式文件',
            //         type: 'warning'
            //     });
            //     return;
            // }
            this.loading = true;
            if (file) {

                const formData = new FormData()
                formData.append('file', file)

                //获取token

                //提交给七牛处理
                this.$http.post('ReportInfomation/file', formData, {
                    progress(event) {
                        //传递给父组件的progress方法
                     
                        // self.$emit('progress', parseFloat(event.loaded / event.total * 100), flag)
                    }
                }).then(response => {
                    var data = response.body;
                    this.loading = false;
                    if (data.code == 1) {

                        this.file = data.msg[0];
                        this.$notify({
                            title: '附件上传成功',
                            message: '附件上传成功',
                            type: 'success'
                        });
                        document.getElementById("fileup").value = "";

                    } else {
                        this.loding = false;
                        this.$notify.error({
                            title: '提交失败',
                            message: data.msg
                        });
                        document.getElementById("fileup").value = "";
                    }
                }, response => {
                    this.loading = false;
                    this.$notify.error({
                        title: '网络错误',
                        message: '网络错误'
                    });
                    this.loding = false;
                    document.getElementById("fileup").value = "";
                })


            }
        },
        submitForm() {

            for (var i = 0; i < this.$refs.dataform.length; i++) {
                for (var k = 0; k < this.$refs.dataform[i].$refs.item.length; k++) {
                    var result = false;
                    this.$refs.dataform[0].$refs.item[0].validate((valid) => {
                        if (valid) {
                            result = true;
                        }
                    });
                   
                    if (!result) {
                        this.$alert('请查看是否有没有填写的必填项', '信息不完整', {
                            confirmButtonText: '确定',
                        });
                        return;
                    }
                }
            }
            if (this.file == "") {
                this.$alert('您还没有上传附件', '信息不完整', {
                    confirmButtonText: '确定',
                });
                return;
            }
            var billresult = {
                BILL_ID: this.$route.query["id"],
                BILL_FILE: this.file,
                BILL_DATA: this.bill,
                BILL_DISEASE: this.BILL_DISEASE,
                BILL_DISEASEDETAIL: this.BILL_DISEASEDETAIL,
                BILL_HISPOTAL: this.BILL_HISPOTAL               

            }

            this.$http.post("Bill/PutNew", billresult).then(response => {
                var data = response.body;
                if (data.code == 1) {
                    this.bill = [];
                    this.$notify({
                        title: '修改成功',
                        message: '修改成功，请等待审核',
                        type: 'success'
                    });
                    this.$router.push({
                        name: 'lipeitask'
                    });

                } else {
                    this.loding = false;
                    this.$notify.error({
                        title: '提交失败',
                        message: data.msg
                    });
                }
            }, response => {
                this.$notify.error({
                    title: '网络错误',
                    message: '网络错误'
                });
                this.loding = false;
            })
       


        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        delectdata(item) {
        
            var detailList=item.BILL_DATA_DETAIL;
            for(var i=0;i<detailList.length;i++){
                detailList[i].BILL_DATA_DETAIL_ISSHOW="删除"
            }
            // var index = this.bill.indexOf(item)
            // if (index !== -1) {
            //     this.bill.splice(index, 1)
            // }
        },
        adddata() {
            if (this.addtime == "") {
                this.$alert('请先选择日期', '提示', {
                    confirmButtonText: '确定',
                });
            } else {
                var data = this.addtime;
              
                var month = data.getMonth() + 1;
               
                var strDate = data.getDate();
                var year = data.getFullYear();
                var time = year + "-" + month + "-" + strDate;
                var have = this.timehave(time);
                if (!have) {
                    this.bill.push({
                        BILL_DATA_ID: getid.getid(),
                        BILL_DATA_DATA: time,
                        BILL_DATA_HNAME: this.BILL_DATA_HNAME,
                        BILL_DATA_TYPE: this.BILL_DATA_TYPE,
                        BILL_DATA_URGENCY: "否",
                        show: time,
                        BILL_DATA_DETAIL: []
                    });
                }
                this.addtime = "";
                this.BILL_DATA_TYPE = "";
            }
        },

        timehave(time) {
            for (var i = 0; i < this.bill.length; i++) {
                if (this.bill[i].show == time) {
                    return true;
                }
            }
            return false;
        },
        getshow(date) {
            var data = new Date(date);         
            var month = data.getMonth() + 1;           
            var strDate = data.getDate();
            var year = data.getFullYear();
            var time = year + "-" + month + "-" + strDate;          
            return time;

        }
    },
    activated() {
      
        this.bill.splice(0, this.bill.length);
        if (this.$route.query["type"] == "审核不通过") {
            this.isediter = true;
        } else {
            this.isediter = false;
        }
        this.$http.get("Bill/" + this.$route.query["id"]).then(response => {
            var data = response.body;
            if (data.code == 1) {
                for (var i = 0; i < data.result[0].BILL_DATA.length; i++) {
                    data.result[0].BILL_DATA[i].show = this.getshow(data.result[0].BILL_DATA[i].BILL_DATA_DATA);
                }
                this.$store.commit('changer', data.result[0].BILL_DATA)
                this.BILL_ID = data.result[0].BILL_ID;
                this.file = data.result[0].BILL_FILE;
                this.BILL_STATE = data.result[0].BILL_STATE;
              
                this.BILL_WHY = data.result[0].BILL_WHY;
                this.BILL_DISEASE = data.result[0].BILL_DISEASE;
                this.BILL_DISEASEDETAIL = data.result[0].BILL_DISEASEDETAIL;
                this.BILL_HISPOTAL = data.result[0].BILL_HISPOTAL;  
            } else {

            }

        }, response => {
            this.$notify.error({
                title: '网络错误',
                message: '网络错误'
            });
            this.loding = false;
        });

    }

}
</script>
<style scoped>
.fileselect {
    overflow: hidden;
    position: relative;
}

.fileselect input {
    opacity: 0;
    position: absolute;
    height: 130%;
}
</style>